package Game;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;

import Game.Enemy.EnemyState;

public class Effect 
{
	public enum EffectType
	{
		PROTESTO,
		EDUCACAO,
		FISCALIZACAO,
		CPI
	}
	
	EffectType type;
	
	boolean active;
	
	boolean discounted;
	
	int duration;
	float c;
	
	Rectangle rect;
	
	GameImage imgEffect;
	
	static int indexCPI = 0;
	
	static Random r = new Random();
	
	public Effect(EffectType type)
	{
		this.type = type;
	}
	
	public Effect(Rectangle rect, EffectType type, int duration)
	{
		this.type = type;
		this.duration = duration;
		this.rect = rect;
		imgEffect = new GameImage(rect.x, rect.y, rect.width, rect.height);
		System.out.println("" + rect.width + " " + rect.height);
		active = true;
		
		switch(type)
		{
			case PROTESTO:
				
				imgEffect.Load("/Images/carta_protesto.png");
				
				break;
			
			case CPI:
				
				imgEffect.Load("/Images/carta_CPI.png");
				
				break;
				
			case EDUCACAO:
				
				imgEffect.Load("/Images/carta_Educacao.png");
				
				break;
				
			case FISCALIZACAO:
				
				imgEffect.Load("/Images/carta_Fiscalizacao.png");
				
				break;
		}
	}
	
	public void Update()
	{
		if(active)
		{
			c++;
			
			if(c >= duration)
			{
				active = false;
				return;
			}
			
			switch(type)
			{
				case PROTESTO:
					
					if(!discounted)
					{
						HUD.RemoveCidadao(2);
						discounted = true;
					}
					
					for(Enemy enemy : EnemyManager.inimigos)
					{
						if(rect.intersects(enemy.getRectangle()))
						{
							enemy.Paralyze(Math.max(5, r.nextInt(20)));
						}
					}
				break;
				
				case CPI:
					
					if(!discounted)
					{
						HUD.RemoveProva(1);
						discounted = true;
					}
					
					AllyManager.AddCPI(new CPI(rect.x + 10, rect.y, 150, 125, 100, indexCPI));
					active = false;
					indexCPI++;
					
					break;
					
				case EDUCACAO:
					
					if(!discounted)
					{
						HUD.RemoveCidadao(2);
						discounted = true;
					}
										
					for(Enemy e : EnemyManager.inimigos)
					{
						if(e.state == EnemyState.MOVING)
						{
							e.Slow();
						}
					}
					active = false;
					break;
					
				case FISCALIZACAO:
					
					if(!discounted)
					{
						HUD.RemoveProva(1);
						discounted = true;
					}
					
					for(Enemy enemy : EnemyManager.inimigos)
					{
						if(rect.intersects(enemy.getRectangle()))
						{
							enemy.Kill(); 
						}
					}
					break;
			}	
		}
	}
	
	public void Draw(Graphics2D g2d)
	{
		if(active)
		{
			imgEffect.Draw(g2d);
		}
	}
}
